/**
 * Created by Administrator on 2016/3/16.
 */
var loadObj;
//返回功能
$(document).ready(function($){
    $('.left_link, .insertBefore_left_arrow_gray').click(function(event){
        //返回判断 如果打开的新窗口的内页(可以直接返回到列表页,根据当前url) | 正常返回不变(go(-1) 返回不刷新,back有刷新功能)
        //window.history.back(-1);
        //window.history.go(-1);
        var currentUri = window.location.href;
        var reg = new RegExp("(^|&)m=([^&]*)(&|$)", "i");
        var m = window.location.search.substr(1).match(reg);
        reg = new RegExp("(^|&)c=([^&]*)(&|$)", "i");
        var c = window.location.search.substr(1).match(reg);
        var uri = "wap.php?m="+m[2]+"&c="+c[2];

        var uri=window.location.pathname;
        if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)){ // IE
            if(history.length > 0)  window.history.go( -1 );
            else window.location.href = uri;
        }else{ //非IE浏览器
            if (navigator.userAgent.indexOf('Firefox') >= 0 ||
                navigator.userAgent.indexOf('Opera') >= 0 ||
                navigator.userAgent.indexOf('Safari') >= 0 ||
                navigator.userAgent.indexOf('Chrome') >= 0 ||
                navigator.userAgent.indexOf('WebKit') >= 0){

                if(history.length > 1)  window.history.go( -1 );
                else window.location.href = uri;
            }else{ //未知的浏览器
                window.location.href = uri;
            }
        }
    });

});



function $ajax(uri, paramObj, func) {
    $.getJSON(uri, paramObj, function(data){
        if (func) func(data, function (data) {
            //显示状态
        });
    });

}

//区域获取关联
function ajaxGetRegion(paramObj,domID,func){
    $.getJSON("./api.php?m=Api&c=index&a=getRegion",paramObj, function(data){
        if(data.status!=0){
            alert("数据获取失败，请稍后重试或联系管理员解决！");
        }else{
            var $obj=$('#'+domID).empty();
            $obj.append("<option value=\"0\">未定</option>");

            $.each(data.data,function(i,item){
                $obj.append("<option value=\""+i+"\">"+item+"</option>");
            });
            if(func){func();}
        }
    });
}

//点赞
function praiseArticle(obj,articleID){
    $.get("./wap.php?m=Wap&c=Article&a=praiseArticle&id="+articleID,function(result){
        if(result.status==0){
            if(typeof result.tip!='undefined'){
                layer.msg(result.tip,{shift:5,offset:$(window).height() * 0.8});
            }
            window.location.reload();
            $(obj).toggleClass('good').toggleClass('good-no')
        }else if(result.status==99){
            window.location.href = './wap.php?m=Wap&c=User&a=login';
        }
    },'json');
}

//点赞评论
function praiseComment(obj,commentID,type){
    window.event.stopPropagation();
    $.post("./wap.php?m=Wap&c=Comment&a=praiseComment&id="+commentID+"&type="+type,function(result){
        if(result.status==0){
            $(obj).html(parseInt($(obj).html())+1);
            $(obj).toggleClass('good').toggleClass('good-no');
            layer.msg(result.msg,{shift:5,offset:$(window).height() * 0.8});
        }else if(result.status==99){
            window.location.href = './wap.php?m=Wap&c=User&a=login';
        }else{
            layer.msg(result.msg);
        }
    },'json');
    return false;
}

//收藏
function collectArticle(obj,aid){
    $.get("./wap.php?m=Wap&c=Article&a=collectArticle&id="+aid,function(result){
        if(result.status==0){
            //window.location.reload();
            $(obj).toggleClass('collection').toggleClass('collection-no');
        }else if(result.status==99){
            window.location.href = './wap.php?m=Wap&c=User&a=login';
        }
    },'json');
}
//加关注
function follow(obj,fid){
    $.get("./wap.php?m=Wap&c=Follow&a=follow&id="+fid,function(result){
        if(result.status==0){
            //切换关注已关注
            var fStatus = $(obj).val();
            var fNum = parseInt($("#num_"+fid).html());

            if(fStatus=="已关注"){
                $(obj).val("+ 关注");
                $("#num_"+fid).html(fNum-1);
            }else{
                $(obj).val("已关注");
                $("#num_"+fid).html(fNum+1);
            }
        }else if(result.status==99){
            window.location.href = './wap.php?m=Wap&c=User&a=login';
        }
    });
}

//验证手机号码
function validatePhone(phone) {
    var reg = /^0?1[3|4|5|7|8|6|9][0-9]\d{8}$/;
    if (!reg.test(phone)) {
        // $("#applyError .list").html('请出入正确的手机号码');
        // $("#applyError").show();
        //alert("请出入正确的手机号码");
        return false;
    }
    return true;
}




function fixUserHead(){
    $(".uhead").one("error", function(){
        if(typeof window.CurrentPlantform =="undefined"){
            $(this).attr("src", "./Public/Wap/images/default_jch.jpg");
        }else{
            $(this).attr("src", "./Public/Wap/images/default_"+window.CurrentPlantform+".jpg");
        }
    });
}

//保留两位小数
//功能：将浮点数四舍五入，取小数点后2位
function toDecimal(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return 0;
    }
    f = Math.round(x*100)/100;
    return f;
}

function __parseInt(value){
    if(!value || isNaN(value))
        return 0;
    return parseInt(value,10);
}
function print_time(time){
    if(time<= 0)	return "00:00:00";
    var h	= __parseInt(time/3600);
    var m	= __parseInt((time-h*3600)/60);
    var	s	= __parseInt(time-h*3600-m*60);
    if(h<10)	h='0'+h;
    if(m<10)	m='0'+m;
    if(s<10)	s='0'+s;
    return h+":"+m+":"+s;
}